// pages/audit/audit.js
const app = getApp()
const api = app.globalData.api

Page({

  /**
   * 页面的初始数据
   */
  data: {
    startDate:'',
    endDate:'',
    date_popup_show:false,
    isBack:false,
    active:0,//底部tabar
    tabIndex:0,//顶部tab
    menu:[],
    keyword:'',
    flowOption:[],
    timeOption:[],
    popup_show:false,
    time_popup_show:false,
    search_state:'',
    search_flowName:'',
    search_flowName_index:0,
    commit_name:'',
    commit_name_id:'',
    commit_time:'',
    commit_time_index:0,
    audit_list:[],//我审批的
    commit_list:[],//我发起的
    stateOption:[],
    audit_popup_show:false,
    page:1,
  },

  
  
  /**开始时间选择 */
  bindStartDateChange(e){
    this.setData({
      startDate: e.detail.value
    })
  },
  /**结束时间选择 */
  bindEndDateChange(e){
    this.setData({
      endDate: e.detail.value
    })
  },
  /**自定义时间选择确认 */
  dateSearch(){
    this.setData({
      commit_time:'',
      date_popup_show: false
    })
    this.requestData();
  },

  chooseStaff(e){
    wx.navigateTo({
      url: '../staff/staff?url=audit',
    })
  },
  showAuditPopup() {
    let that = this;
    api.curlPost('?cla=auditApp&fun=search').then((res)=>{
      that.setData({
        audit_popup_show:true,
        stateOption:res.data.result.stateOption
      })
    })
  },
  onStateConfirm(event){
    const { picker, value, index } = event.detail;
    this.setData({
      search_state:`${value}`,
      audit_popup_show:false
    })
    this.requestData();
  },
  onStateCancel(){
    this.setData({
      audit_popup_show:false
    })
  },
  /**显示流程选项搜索 */
  showPopup() {
    let that = this;
    api.curlPost('?cla=auditApp&fun=search').then((res)=>{
      that.setData({
        popup_show:true,
        flowOption:res.data.result.flowOption
      })
    })
  },
  showTimePopup(){
    let that = this;
    api.curlPost('?cla=auditApp&fun=search').then((res)=>{
      that.setData({
        time_popup_show:true,
        timeOption:res.data.result.timeOption
      })
    })
  },
  closePopup(){
    this.setData({ popup_show: false });
  },
  /**流程选项弹出层关闭 */
  onFlowCancel(e){
    this.closePopup();
  },
  /**流程搜索选项切换 */
  onFlowConfirm(event){
    const { picker, value, index } = event.detail;
    this.setData({
      search_flowName:`${value}`
    })
    this.requestData();
    this.closePopup();
  },
  /**时间选项弹出层关闭 */
  onTimeCancel(e){
    this.setData({
      time_popup_show:false
    })
  },
  /**时间搜索选项切换 */
  onTimeConfirm(event){
    const { picker, value, index } = event.detail;
    if(value=='自定义'){
      this.setData({
        date_popup_show:true,
        time_popup_show:false
      })
    }else{
      this.setData({
        commit_time:`${value}`,
        startDate:'',
        endDate:'',
        time_popup_show:false
      })
      this.requestData();
    }
  },
  /**tabar切换 */
  onChange(event){
    this.clearCondition();
    let index = event.detail;
    this.setData({
      active: index,
    })
    this.requestData();
  },
  /**标签页切换 */
  tabChange(e){
    //清空搜索条件
    this.clearCondition();
    this.setData({
      tabIndex:e.detail.name
    })
    this.requestData();
  },
  /**清空条件 */
  clearCondition(){
    this.setData({
      search_flowName:'',
      search_flowName_index:0,
      commit_name:'',
      commit_name_id:'',
      commit_time:'',
      commit_time_index:0,
      keyword:'',
      startDate:'',
      endDate:'',
      search_state:'',
    })
  },
  /**搜索 */
  onSearch(e){
    this.requestData();
  },
  /**搜索文字变动 */
  searchChange(e){
    this.setData({
      keyword: e.detail,
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.requestData();
  },

  detail(e){
    let url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url:url,
    })
  },
  requestData(page=1){
    let that = this;
    let para = {
      keyword:this.data.keyword,
      flowName:this.data.search_flowName,
      applicantId:this.data.commit_name_id,
      startDate:this.data.startDate,
      endDate:this.data.endDate,
      commit_time:this.data.commit_time,
      search_state:this.data.search_state
    }
    if(this.data.active==0){
      api.curlPost('?cla=auditApp&fun=home').then((res)=>{
        that.setData({
          menu:res.data.result.menu
        })
        wx.stopPullDownRefresh()
      })
    }else if(this.data.active==1){
      if(this.data.tabIndex==0){
        api.curlPost('?cla=auditApp&fun=waitlist&page='+page,para).then((res)=>{
          if(!api.isBlank(res.data.result) || page ==1){
            let arr = page==1?res.data.result:that.data.audit_list.concat(res.data.result);
            that.setData({
              audit_list:arr,
              page:Number(res.data.page.pageNow) +1
            })
            wx.stopPullDownRefresh()
          }
        })
      }else if(this.data.tabIndex==1){
        api.curlPost('?cla=auditApp&fun=approveed&page='+page,para).then((res)=>{
         
          if(!api.isBlank(res.data.result) || page ==1){
            let arr = page==1?res.data.result:that.data.audit_list.concat(res.data.result);
            that.setData({
              audit_list:arr,
              page:Number(res.data.page.pageNow) +1
            })
            wx.stopPullDownRefresh()
          }
        })
      }else if(this.data.tabIndex==2){
        api.curlPost('?cla=auditApp&fun=copyMe&page='+page,para).then((res)=>{
          if(!api.isBlank(res.data.result) || page ==1){
            let arr = page==1?res.data.result:that.data.audit_list.concat(res.data.result);
            that.setData({
              audit_list:arr,
              page:Number(res.data.page.pageNow) +1
            })
            wx.stopPullDownRefresh()
          }
        })

        
      }
      
    }else if(this.data.active==2){
      api.curlPost('?cla=auditApp&fun=myApply&page='+page,para).then((res)=>{

        if(!api.isBlank(res.data.result) || page ==1){
          let arr = page==1?res.data.result:that.data.commit_list.concat(res.data.result);
          that.setData({
            commit_list:arr,
            page:Number(res.data.page.pageNow) +1
          })
          wx.stopPullDownRefresh()
        }
       
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if(this.data.isBack){
      this.setData({
         isBack:false
      })
      this.requestData();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.clearCondition();
    this.requestData()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.requestData(this.data.page)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})